package cn.ligoo.part.web.springmvc.platform;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.MessageSource;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.ligoo.part.domain.SysAction;
import cn.ligoo.part.service.impl.FacadeImpl;
import cn.ligoo.part.web.Constants;

import com.alibaba.fastjson.JSON;

@Controller
@RequestMapping("/platform/sys-action")
public class SysActionController {

	private final Logger logger = LoggerFactory.getLogger(SysActionController.class);

	@Resource
	private MessageSource message;

	@Resource
	private FacadeImpl facade;

	public SysActionController() {
		logger.debug("...constructor");
	}

	@RequestMapping(value = "/search", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String search(HttpServletRequest request, @RequestParam int page, @RequestParam int rows, SysAction t) {
		logger.debug("...search");

		logger.debug("...page:{}, rows:{}", page, rows);
		logger.debug("...sysAction:{}", t.toString());

		Long total = facade.getSysActionService().getSysActionCount(t);

		t.get_rowBounds().setOffset((page - 1) * rows);
		t.get_rowBounds().setLimit(rows);
		List<SysAction> sysActionList = facade.getSysActionService().getSysActionList(t);

		String json = String.format(Constants.JSON_DATAGRID, JSON.toJSONString(sysActionList), total);

		logger.debug("...json:{}", json);
		return json;
	}

	@RequestMapping(value = "/delete", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String delete(@RequestParam String[] ids) {
		logger.debug("...delete ids:{}", StringUtils.join(ids, ","));
		SysAction t = new SysAction();
		t.get_map().put("_ids", ids);
		t.get_map().put("_isLogicalDelete", "true");

		logger.debug("...sysAction:{}", t);
		int count = 0;
		try {
			count = facade.getSysActionService().removeSysAction(t);
		} catch (DataAccessException dae) {
			String msg = message.getMessage("errors.dae", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		}
		return String.format(Constants.JSON_SUCCESS, null, count);
	}

	@RequestMapping(value = "/save", produces = "text/html;charset=UTF-8", method = RequestMethod.POST)
	@ResponseBody
	public String save(SysAction sysAction) {
		logger.debug("...save");
		logger.debug("sysAction:{}", sysAction.toString());
		try {
			if (null == sysAction.getId()) {
				facade.getSysActionService().createSysAction(sysAction);
			} else {
				facade.getSysActionService().modifySysAction(sysAction);
			}
		} catch (DuplicateKeyException dke) {
			String msg = message.getMessage("errors.dae.DuplicateKeyException", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		} catch (DataAccessException dae) {
			String msg = message.getMessage("errors.dae", null, null);
			return String.format(Constants.JSON_ERROR, msg);
		}
		return String.format(Constants.JSON_SUCCESS, null, null);
	}

	@RequestMapping(value = "/edit/{id}", produces = "text/html;charset=UTF-8", method = RequestMethod.GET)
	@ResponseBody
	public String edit(@PathVariable Long id) {
		logger.debug("...edit");
		SysAction t = new SysAction();
		t.setId(id);
		SysAction sysAction = facade.getSysActionService().getSysAction(t);
		logger.debug("...sysAction:{}", sysAction);
		return JSON.toJSONString(sysAction);
	}
}
